﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using Entities;

namespace eestec.Controllers.API
{
    public class ParticipantsController : BaseController
    {

        // GET api/Participants
        //public IEnumerable<User> GetUsers()
        //{
        //    return db.Users.AsEnumerable();
        //}

        // GET api/Participants/5
        public IEnumerable<User> GetParticipants(int id)
        {
            Event e = db.Events.Find(id);
            if (e == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }
            return db.Users.Where(u => u.Events.Any(p=>p.EventId == id)).AsEnumerable();
            
        }

        // PUT api/Participants/5
        //public HttpResponseMessage PutUser(string id, User user)
        //{
        //    if (ModelState.IsValid && id == user.EMail)
        //    {
        //        db.Entry(user).State = EntityState.Modified;

        //        try
        //        {
        //            db.SaveChanges();
        //        }
        //        catch (DbUpdateConcurrencyException)
        //        {
        //            return Request.CreateResponse(HttpStatusCode.NotFound);
        //        }

        //        return Request.CreateResponse(HttpStatusCode.OK);
        //    }
        //    else
        //    {
        //        return Request.CreateResponse(HttpStatusCode.BadRequest);
        //    }
        //}

        // POST api/Participants
        public HttpResponseMessage PostUser(string email, int eventId)
        {
            if (ModelState.IsValid)
            {
                User user = db.Users.Find(email);
                Event e = db.Events.Find(eventId);
                if (e == null)
                {
                    throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
                }
               

                User participant = db.Users.Find(user.EMail);
                if (participant == null)
                {
                    db.Users.Add(user);
                    e.Participants.Add(user);
                }
                else if(!db.Events.Where(p=>p.EventId == eventId && p.Participants.Any(u=>u.EMail == user.EMail)).Any())
                {
                    e.Participants.Add(participant);
                }
                
                db.SaveChanges();

                HttpResponseMessage response = null;
                
                if(Request != null) response = Request.CreateResponse(HttpStatusCode.Created, user);
                //response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = user.EMail }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // DELETE api/Participants/5
        public HttpResponseMessage DeleteUser(string id)
        {
            User user = db.Users.Find(id);
            if (user == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.Users.Remove(user);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return Request.CreateResponse(HttpStatusCode.OK, user);
        }
    }
}